package org.ourgrid.broker.business.scheduler;

import org.ourgrid.broker.business.scheduler.workqueue.SchedulerDataCollector;
import org.ourgrid.broker.business.scheduler.workqueue.WorkQueueReplication;
import org.ourgrid.broker.business.scheduler.workqueue.xmlcreator.LoggerXMLCreator;
import org.ourgrid.broker.business.scheduler.workqueue.xmlcreator.XMLCreatorFactory;
import org.ourgrid.broker.business.scheduler.workqueue.xmlcreator.XMLCreatorIF;
import org.ourgrid.broker.business.schedulerdata.SchedulerData;

import br.edu.ufcg.lsd.commune.container.logging.CommuneLogger;

public class LoggerAdapter extends CommuneLogger {

	public LoggerAdapter() {
		super(WorkQueueReplication.class);
	}
	
	private SchedulerDataCollector getCollector() {
		return SchedulerDataCollector.getInstance();
	}

	private SchedulerData createData(String msg, int type) {
		return new SchedulerData(getXmlCreator(LoggerXMLCreator.class).getXML(msg, type)); 
	}
	
	public void enter(){
		getCollector().addData(createData("Entering " + getMethodName(), LoggerXMLCreator.TRACE));
	}
	
	public void leave(){
		getCollector().addData(createData("Exiting " + getMethodName(), LoggerXMLCreator.TRACE));
	}
	
	public void warnException(Exception e){
		getCollector().addData(createData("Beware... " + e.getMessage(), LoggerXMLCreator.WARN));
	}
	
	public void exception(Exception e){
		getCollector().addData(createData("An Error Ocurred:  " + e.getMessage(), LoggerXMLCreator.ERROR));
	}
	
	public void info(Object msg){
		getCollector().addData(createData(msg.toString(), LoggerXMLCreator.INFO));
	}
	
	public void error(Object msg){
		getCollector().addData(createData(msg.toString(), LoggerXMLCreator.ERROR));
	}

	public void error(Object msg, Exception e){
		getCollector().addData(createData(msg.toString() + e.getMessage(), LoggerXMLCreator.ERROR));
	}
	
	/**
	 * @param message
	 * @see org.apache.log4j.Category#debug(java.lang.Object)
	 */
	public void debug(Object message) {
		getCollector().addData(createData(message.toString(), LoggerXMLCreator.DEBUG));
	}


	/**
	 * @param message
	 * @param t
	 * @see org.apache.log4j.Category#fatal(java.lang.Object, java.lang.Throwable)
	 */
	public void fatal(Object message, Throwable t) {
		getCollector().addData(createData(message.toString() + t.getMessage(), LoggerXMLCreator.FATAL));
	}


	/**
	 * @param message
	 * @see org.apache.log4j.Logger#trace(java.lang.Object)
	 */
	public void trace(Object message) {
		getCollector().addData(createData(message.toString(), LoggerXMLCreator.TRACE));
	}


	/**
	 * @param message
	 * @see org.apache.log4j.Category#warn(java.lang.Object)
	 */
	public void warn(Object message) {
		getCollector().addData(createData(message.toString(), LoggerXMLCreator.WARN));
	}


	public void debug(String string, Exception e) {
		getCollector().addData(createData(string + e.getMessage(), LoggerXMLCreator.DEBUG));
	}
	
	private <U extends XMLCreatorIF> U getXmlCreator(Class<U> clazz) {
		return XMLCreatorFactory.getInstance().getXMLCreator(clazz);
	}

}
